﻿#include <QDialog>
#include <QFile>
#include <QMouseEvent>
#include "CVideoPlayer.h"
#include "CGlobal.h"
#include "CVideoLargeDlg.h"
#include "CGlobal.h"
#include "Public.h"

CVideoPlayer::CVideoPlayer(CDevBeltRealign* pDev)
	:m_pDev(pDev)
{
	ui.setupUi(this);
	m_bRun = false;
	m_bIsPlaying = false;
	initConnect();
}

CVideoPlayer::~CVideoPlayer()
{
	endPlayVideo();
}

void CVideoPlayer::initConnect()
{
	connect(ui.pushButtonVideoPlay, &QPushButton::clicked, this, &CVideoPlayer::slotPushPlay);
	connect(ui.pushButtonLarge, &QPushButton::clicked, this, &CVideoPlayer::slotPushLarge);
	connect(ui.pushButtonPic, &QPushButton::clicked, this, &CVideoPlayer::slotPushSnop);
}

void CVideoPlayer::setStyleSheetFile()
{
	QString sQssPath = ":/qss/styleSheet.css";
	QFile qss(sQssPath);
	qss.open(QFile::ReadOnly);
	this->setStyleSheet(qss.readAll());
}

bool CVideoPlayer::startPlayVideo()
{
	if (m_bRun == false)
		return false;
	if (m_bIsPlaying)
		return true;
	SCtrInfo infoTx;
	infoTx.m_pLabel = ui.label;
	infoTx.m_hWid = ui.label->winId();
	infoTx.m_sGuid = m_pDev->m_sPointGuid;
	infoTx.m_nCtrCmd = enCRealPlay;
	infoTx.m_nCtrDevType = enSubDev1;
	infoTx.m_nSubCtrDevType = enCamer;
	bool bTxRet = m_pDev->ctrDev(infoTx);
	if (bTxRet == true)
	{
		m_bIsPlaying = true;
		ui.pushButtonVideoPlay->setIcon(QIcon(":/icon/StopPlay.png"));
	}
	return bTxRet;
}

bool CVideoPlayer::endPlayVideo()
{
	if (m_bIsPlaying == false)
		return true;
	SCtrInfo infoTx;
	infoTx.m_pLabel = ui.label;
	infoTx.m_hWid = ui.label->winId();
	infoTx.m_sGuid = m_pDev->m_sPointGuid;
	infoTx.m_nCtrCmd = enCStopRealPlay;
	infoTx.m_nCtrDevType = enSubDev1;
	infoTx.m_nSubCtrDevType = enCamer;
	bool bTxRet = m_pDev->ctrDev(infoTx);
	if (bTxRet == true)
	{
		m_bIsPlaying = false;
		ui.pushButtonVideoPlay->setIcon(QIcon(":/icon/play.png"));
	}
	return bTxRet;
}

void CVideoPlayer::slotPushPlay()
{
	if (m_bRun == false)
		return;
	if (m_bIsPlaying)//如果正在播放视频
	{
		endPlayVideo();
		
	}
	else//则播放当前选中的节点
	{
		startPlayVideo();
		
	}
}

void CVideoPlayer::slotPushLarge()
{
	CVideoLargeDlg oper(m_pDev,0);
	oper.exec();
}

void CVideoPlayer::slotPushSnop()
{
	if (!m_pDev->m_pDevData->m_pDevDataCam->m_bLogin)
		return;
	QString sImgUrl = m_pDev->m_pDevData->m_sRecordPath + "Image/";
	SCtrInfo infoTx;
	infoTx.m_sGuid = m_pDev->m_sPointGuid;
	infoTx.m_nCtrCmd = enCGetImage;
	infoTx.m_nCtrDevType = enSubDev1;
	infoTx.m_nSubCtrDevType = enCamer;
	infoTx.m_bCtrSync = true;
	infoTx.m_sImgUrl = sImgUrl;
	bool bTxRet = m_pDev->ctrDev(infoTx);
	if (bTxRet)
	{
		messageBox(0, A2T("图片抓拍成功，图片保存路径:%1").arg(infoTx.m_sImgUrl));
		return;
	}
	else
	{
		messageBox(1, A2T("图片抓拍失败"));
	}
}

void CVideoPlayer::refreshData(bool bRefresh)
{
	m_bRun = bRefresh;
	if (bRefresh)
	{
		if (!m_pDev->m_pDevData->m_pDevDataCam->m_bLogin)
		{
			return;
		}
		if (m_bIsPlaying == false)
		{
			startPlayVideo();
		}
	}
	else
	{
		endPlayVideo();
	}
}
